package com.zhazha.stand.controller.admin;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import com.zhazha.stand.controller.RestfulController;
import com.zhazha.stand.logic.impl.DepartmentLogicImpl;
import com.zhazha.stand.entity.Department;

public class DepartmentController extends RestfulController{

	private Logger logger = Logger.getLogger(DepartmentController.class);
	private DepartmentLogicImpl dmLogic = new DepartmentLogicImpl();
	private List<Department> dmList = null;
	private Department department = new Department();
	
	
	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	@Override
	public String create() {
		
		Department dm = new Department();
		dm.setName(request.getParameter("departmentName"));
		logger.info("Department Create:" + dm.getName());
		if (!dmLogic.insert(dm)) {
			return ERROR;
		}
		try {
			response.sendRedirect("department.action");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String destroy() {
		int id = this.getIntParam("id");
		logger.info("Department Destroy:" + String.valueOf(id));
		if (!dmLogic.delete(id))
			return ERROR;
		try {
			response.sendRedirect("department.action");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String xml() {
		// TODO Auto-generated method stub
		return XML;
	}

	@Override
	public String json() {
		// TODO Auto-generated method stub
		return JSON;
	}

	/**
	 * @return the dmList
	 */
	public List<Department> getDmList() {
		return dmList;
	}

	/**
	 * @param dmList the dmList to set
	 */
	public void setDmList(List<Department> dmList) {
		this.dmList = dmList;
	}

	@Override
	public String update() {
		int id = this.getIntParam("id");
		logger.info("update:" + String.valueOf(id));
		Department t = new Department();
		t.setId(id);
		t.setName(request.getParameter("departmentName"));
		dmLogic.update(t);
		try {
			response.sendRedirect("department.action");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String index() {
		this.dmList = dmLogic.getList();
		logger.info("index");
		return INDEX;
	}

	@Override
	public String editNew() {
		// TODO Auto-generated method stub
		this.resourceMethod = "";
		this.resourceId = "";
		logger.info("editNew");
		return EDITNEW;
	}

	@Override
	public String edit() {
		int id = this.getIntParam("id");
		department = new Department();
		department.setId(id);
		department.setName(request.getParameter("departmentName"));
		
		this.resourceMethod = "put";
		this.resourceId = String.valueOf(id);
		logger.info("edit");
		
		return EDIT;
	}
	
	@Override
	public String show() {
		// TODO Auto-generated method stub
		return SHOW;
	}

}
